import { observable, action, toJS } from 'mobx';
import { WeaForm } from 'comsMobx';
import API from '../../apis';
import { message } from 'antd';
import { listStore } from '.';
import { WeaLocaleProvider } from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

export interface TemplateStoreProps {
    form: any;
    fields: any[];
    errors: any;
    clearError: (domkey: string) => void;
    loading?: boolean;
    onOk: (e) => void;
    onCancel: (e) => void;
    saveAsTemplate: () => void;
    visible: boolean;
    managerTemplate: (Search: listStore) => void;
}

export default class TemplateStore implements TemplateStoreProps {
    @observable form = new WeaForm();
    @observable visible = false;
    @observable loading = false;
    @observable fields: any = [];
    @observable
    errors = {
        templatename: '',
        templatetype: '',
        fieldorder: ''
    };
    @action
    onCancel = () => {
        this.form.resetConditionValue();
        this.visible = false;
    }
    @action
    onOk = (Search) => {
        this.loading = true;
        this.form.validateForm().then(f => {
            if (f.isValid) {
                this.saveTemplate(Search, ({ templates }) => {
                    if (Search.type == 'advanced') {
                        Search.advanced.templates = templates;
                    } else {
                        Search.condition.templates = templates;
                    }
                    this.onCancel();
                    this.loading = false;
                }, 'add', this.form.values());
            } else {
                f.showErrors();
                this.loading = false;
                // message.error(getLabel(383975, '必要信息不完整，红色标记为必填项！'));
            }
        });
    };
    @action
    saveTemplate = (Search: listStore, callback?: (props: any) => void, action: string = 'save', others: any = {}) => {
        const getValues = (conditionInfo, values) => {
            let vi = toJS(values);
            let ci = toJS(conditionInfo[0].items);
            let rs = ci.map(c => `${c.domkey}`)
            let vs = rs.map(c => `${vi[c]}`);
            return JSON.stringify({ rs, vs });
        };
        API.saveTemplate({
            action: action,
            customid: Search.customId,
            templateid: Search.templateid,
            sourcetype: Search.type == 'advanced' ? '1' : '2',
            ...others,
            type: Search.type == 'advanced' ? '1' : ['1', '2'],
            values: getValues(Search.type == 'advanced' ? Search.advancedInfo : Search.conditionInfo, Search.type == 'advanced' ? Search.advanced.form.values() : Search.condition.form.values())
        }).then(({ status, error, ...props }) => {
            if (status == '1') {
                message.success(getLabel(18758, "保存成功。"));
            } else {
                message.error(error)
                throw error;
            }
            callback && callback(props);
        })
    }
    @action
    clearError = (domkey: string) => {
        this.errors[domkey] = '';
    }
    @action
    saveAsTemplate = () => {
        this.fields = [{
            colSapn: 2,
            conditionType: 'INPUT',
            domkey: ['templatename'],
            fieldcol: 14,
            labelcol: 6,
            label: getLabel(18151, '模板名称'),
            viewAttr: 3,
            'rules': 'required|string',
        }, {
            colSapn: 2,
            conditionType: 'SELECT',
            domkey: ['templatetype'],
            fieldcol: 14,
            labelcol: 6,
            label: getLabel(19471, '模板类型'),
            viewAttr: 3,
            options: [{ key: '0', showname: getLabel(83139, '私人模板'), selected: true }]
        }, {
            colSapn: 2,
            conditionType: 'INPUTNUMBER',
            domkey: ['fieldorder'],
            fieldcol: 14,
            labelcol: 6,
            label: getLabel(88, '顺序'),
            viewAttr: 2,
            precision: 2
        }, {
            colSapn: 2,
            conditionType: 'CHECKBOX',
            domkey: ['isdefault'],
            fieldcol: 14,
            labelcol: 6,
            label: getLabel(33731, '是否默认', 'label'),
            viewAttr: 2,
        }];
        if (this.form.isFormInit) {
            this.form.resetConditionValue();
        } else {
            this.form.initFormFieldObj(toJS(this.fields));
        }
        this.visible = true;
    }
    managerTemplate = (Search: listStore) => {
        //window.open(`/formmode/template/TemplateManage.jsp?customid=${Search.customId}&sourcetype=${Search.type == 'advanced' ? '1' : '2'}`);
        window.open(`/spa/cube/index.html#/main/cube/templateManage?customid=${Search.customId}&sourcetype=${Search.type == 'advanced' ? '1' : '2'}`);
    }
    @action
    update = (param: any) => {
        for (let key in param) {
            this[key] = param[key]
        }
    }
} 